﻿using System;
using System.Collections.Generic;
using System.Web;
<<<<<<< .mine
using System.IO;
using System.Data;
using System.Text;
=======
using System.IO;
>>>>>>> .r100

namespace OPLService
{
    /// <summary>
    /// Summary description for OPLService
    /// </summary>
    public class OPLService : IHttpHandler
    {

        #region ProcessRequest
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            context.Response.Write(Process(context.Request));
        }
        #endregion

        #region Properties
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
        #endregion
<<<<<<< .mine

        private string Process(HttpRequest request)
        {

            string result = "Invalid Query!!";;
            
            switch (Path.GetFileName(request.Path).ToLower())
            {
                case "getnotifications":
                    {
                        string[] paras = GetQueryVars(request.QueryString);
                        if (paras.Length == 2)
                        {
                            
                            DateTime endDate= new DateTime(1970, 1, 1, 0, 0, 0);
                            double secsFrom1970 = double.Parse(paras[0]);
                            double secsInterval = double.Parse(paras[1]);
                            endDate = endDate.AddMilliseconds(secsFrom1970);
                            DateTime startDate = endDate.AddMilliseconds(-secsInterval);
             
                            JoymonsDB.NotificationsDataTable resultTable = GetData(startDate, endDate);
                            string jSon = GetSerializedJson(resultTable);
                            result = jSon;
                        }
                        //result = new NotificationDetails() { Caption = "Hello", Description = "World!!", Id = 1, NotifiedOn = DateTime.Now };
                        //result="{\"Error\":0,\"ErrorMessage?\":\"\",\"Result\":[ {\"type\":\"NotificationDetails?:#OPLService.BusinessEntities?\",\"Caption\":\"TestCaption1?\",\"Description\":\"TestDesc1?\",\"Id\":1}, {\"type\":\"NotificationDetails?:#OPLService.BusinessEntities?\",\"Caption\":\"TestCaption2?\",\"Description\":\"TestDesc2?\",\"Id\":2}, {\"type\":\"NotificationDetails?:#OPLService.BusinessEntities?\",\"Caption\":\"TestCaption3?\",\"Description\":\"TestDesc3?\",\"Id\":3}, {\"type\":\"NotificationDetails?:#OPLService.BusinessEntities?\",\"Caption\":\"TestCaption4?\",\"Description\":\"TestDesc4?\",\"Id\":4}, {\"type\":\"NotificationDetails?:#OPLService.BusinessEntities?\",\"Caption\":\"TestCaption5?\",\"Description\":\"TestDesc5?\",\"Id\":5}]}";
                        break;
                    }
                default:
                    {
                        
                        break;
                    }
            }
            return result;
            //return JsonSerialize(result);
        }

        private string GetSerializedJson(JoymonsDB.NotificationsDataTable resultTable)
        {
           // JsonSerializer ser = new JsonSerializer();
            StringBuilder builder = new StringBuilder();
            builder.Append("{\"Error\":0,\"ErrorMessage\":\"\",\"Result\":[ ");
            //JsonTextWriter writer = new JsonTextWriter(new StringWriter(builder));
            //writer.WriteStartArray();
            foreach( JoymonsDB.NotificationsRow row in resultTable)
            {
                builder.Append("{");

                builder.AppendFormat("\"{0}\":\"{1}\",", "Caption",row.Caption); //\"Caption\":\"TestCaption1?\"
                builder.AppendFormat("\"{0}\":\"{1}\",", "Id", row.Id);
                builder.AppendFormat("\"{0}\":\"{1}\"", "Description", row.Description);
                //builder.AppendFormat("\"{0}\":\"{1}\"", "NotifiedOn", row.NotifiedOn);
                builder.Append("},");
            }
            if(resultTable.Count>0) builder.Remove(builder.Length - 1, 1);
            builder.Append("]}");
            //writer.WriteEndArray();
            string res = builder.ToString();
            return res;

        }

        private JoymonsDB.NotificationsDataTable GetData(DateTime startDate, DateTime endDate)
        {
            JoymonsDBTableAdapters.NotificationsTableAdapter ada = new JoymonsDBTableAdapters.NotificationsTableAdapter();
            JoymonsDB.NotificationsDataTable table = ada.GetDataByNotifiedOn(startDate, endDate);
            return table;
            
        }

        private string[] GetQueryVars(System.Collections.Specialized.NameValueCollection nameValueCollection)
        {
            string []res=new string[2];
            res[0]=nameValueCollection[0];
            res[1] = nameValueCollection[1];
            return res;
        }

        private string JsonSerialize(object result)
        {
            string serializedString = string.Empty;
            if (result.GetType().GetInterface("IJsonSerialize")!=null)
            {
                serializedString = ((IJsonSerialize)result).Serialize();
            }
            else
            {
                serializedString = result.ToString();
            }
            return serializedString;
        }
=======

        private string Process(HttpRequest request)
        {
            object result = string.Empty;
            switch (Path.GetFileName(request.Path).ToLower())
            {
                case "getnotifications":
                    {
                        result = new NotificationDetails() { Caption = "Hello", Description = "World!!", Id = Convert.ToInt32(request.QueryString["value"]), NotifiedOn = DateTime.Now };
                        break;
                    }
                default:
                    {
                        result = "Invalid Query!!";
                        break;
                    }
            }
            return JsonSerialize(result);
        }

        private string JsonSerialize(object result)
        {
            string serializedString = string.Empty;
            if (result.GetType().GetInterface("IJsonSerialize")!=null)
            {
                serializedString = ((IJsonSerialize)result).Serialize();
            }
            else
            {
                serializedString = result.ToString();
            }
            return serializedString;
        }
>>>>>>> .r100
    }
}